package com.avic.rong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.bean.GetOrderTracesByJsonAllBean;
import com.avic.rong.utils.GlideUtils;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;
import com.example.minato.minastore.utils.GlideApp;

import java.util.ArrayList;

/**
 * Created by XinEr on 2019/7/10.
 * Describe:
 */
public class RollOrderListAdapter extends RecyclerView.Adapter<RollOrderListAdapter.MyViewHolder> implements View.OnClickListener {

    private Context context;
    private ArrayList<GetOrderTracesByJsonAllBean.OrderTracesListBean.TracesListBean> wuliuListData;



    public RollOrderListAdapter(Context context, ArrayList<GetOrderTracesByJsonAllBean.OrderTracesListBean.TracesListBean> wuliuListData) {
        this.wuliuListData = wuliuListData;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_roll_item, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        myViewHolder.itemView.setOnClickListener(this);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int i) {
        if (wuliuListData == null || wuliuListData.size() == 0) {
            return;
        }
        if (holder != null) {
            final int position = i % wuliuListData.size();
            GetOrderTracesByJsonAllBean.OrderTracesListBean.TracesListBean tracesListBean = wuliuListData.get(position);

            if (tracesListBean.getOrderType() == 4) {
                if (!TextUtils.isEmpty(tracesListBean.getProImg())) {
                    if (tracesListBean.getProImg().contains("rongrong.")) {
                        GlideUtils.loaderImage(context, tracesListBean.getProImg(),holder.iv_pic );
                    } else {
                        GlideUtils.loaderImage(context, tracesListBean.getBiaoZhunImg() + tracesListBean.getProImg(), holder.iv_pic );
                    }
                    holder.iv_pic .setVisibility(View.VISIBLE);
                    holder.ll_pic_tag.setVisibility(View.GONE);
                } else {
                    String keywords = tracesListBean.getKeywords();
                    String catName = tracesListBean.getCatName();
                    holder.tv_goods_type.setText(catName);
                    holder.tv_goods_tel.setText(keywords);
                    holder.ll_pic_tag.setVisibility(View.VISIBLE);
                    holder.iv_pic .setVisibility(View.VISIBLE);
//                    Glide.with(context).load(R.mipmap.common_bg).into(holder.iv_pic );
                    GlideApp.with(context).load(R.mipmap.common_bg).centerCrop().into(holder.iv_pic);
                }
            } else {
                if (!TextUtils.isEmpty(tracesListBean.getProImg())) {
                    GlideUtils.loaderImage(context, tracesListBean.getProImg(), holder.iv_pic );
                    holder.iv_pic .setVisibility(View.VISIBLE);
                    holder.ll_pic_tag.setVisibility(View.GONE);
                } else {
                    //默认图
//                GlideUtils.loaderImage(context, R.mipmap.common_bg, iv_goods_pic);
                    String keywords = tracesListBean.getKeywords();
                    String catName = tracesListBean.getCatName();
                    holder.tv_goods_type.setText(catName);
                    holder.tv_goods_tel.setText(keywords);
//                    Glide.with(context).load(R.mipmap.common_bg).into(holder.iv_pic );
                    GlideApp.with(context).load(R.mipmap.common_bg).centerCrop().into(holder.iv_pic);
                    holder.iv_pic .setVisibility(View.VISIBLE);
                    holder.ll_pic_tag.setVisibility(View.VISIBLE);
                }

            }



//            GlideUtils.loaderImage(context, tracesListBean.getProImg(), holder.iv_pic);
            if (tracesListBean.getTraces() != null && tracesListBean.getTraces().size() > 0) {
                holder.tv_order_address.setText(tracesListBean.getTraces().get(0).getAcceptStation());
            } else {
                holder.tv_order_address.setText("");
            }

//            Glide.with(context).load(tracesListBean.getProImg()).into(holder.iv_pic);
//            无轨迹，已揽收，在途中，这三类用货车的icon；签收，用现在的对勾icon；问题件，用问号icon
//        物流状态0： 无轨迹，1：已揽收，2：在途中，3：签收，4：问题件
            if (tracesListBean.getState() != null) {
                if (tracesListBean.getState().equals("0")) {
                    holder.tv_order_tag.setText("无轨迹");
                    holder.tv_order_tag.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.ic_transit_car),null,null,null);
                } else if (tracesListBean.getState().equals("1")) {
                    holder.tv_order_tag.setText("已揽收");
                    holder.tv_order_tag.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.ic_transit_car),null,null,null);
                } else if (tracesListBean.getState().equals("2")) {
                    holder.tv_order_tag.setText("在途中");
                    holder.tv_order_tag.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.ic_transit_car),null,null,null);
                } else if (tracesListBean.getState().equals("3")) {
                    holder.tv_order_tag.setText("签收");
                    holder.tv_order_tag.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.ic_complate),null,null,null);
                } else if (tracesListBean.getState().equals("4")) {
                    holder.tv_order_tag.setText("问题件");
                    holder.tv_order_tag.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.mipmap.ic_logistics_error),null,null,null);
                }


            }
            holder.itemView.setTag(position);
        }

    }

    @Override
    public int getItemCount() {
//        return wuliuListData == null ? 0 : wuliuListData.size();
        return wuliuListData == null ? 0 : Integer.MAX_VALUE;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_pic;
        private TextView tv_order_tag;
        private TextView tv_order_address;

        private ImageView iv_goods_pic;
        private TextView tv_goods_type;
        private TextView tv_goods_tel;
        private LinearLayout ll_pic_tag;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv_pic = itemView.findViewById(R.id.iv_goods_pic);
            tv_order_tag = itemView.findViewById(R.id.tv_order_tag);
            tv_order_address = itemView.findViewById(R.id.tv_order_address);
            tv_goods_type = itemView.findViewById(R.id.tv_goods_type);
            tv_goods_tel = itemView.findViewById(R.id.tv_goods_tel);
            ll_pic_tag = itemView.findViewById(R.id.ll_pic_tag);
        }

    }

    public interface OnClickItem {
        void OnClickListion(View view, int postion);
    }

    private OnClickItem onClickItem;

    @Override
    public void onClick(View v) {
        if (v != null) {
            onClickItem.OnClickListion(v, (int) v.getTag());
        }
    }

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }

}

